<template>
  <div>
    <!-- 电吹风 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="closeDialog"
      title="电吹风信息"
      :close-on-click-modal="false"
      :visible.sync="eidtDialogFormVisible"
    >
      <el-form
        :model="editdataForm"
        :rules="eidtataRule"
        ref="editdataForm"
        @keyup.enter.native="addataFormSubmit()"
        label-width="110px"
        :disabled="!isauth('hairdrider_edit')"
      >
        <el-form-item label="设备名称" prop="terminalName">
          <el-col :span="15">
            <el-input v-model="editdataForm.terminalName" size="mini" disabled></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="设备编号" prop="terminalCode">
          <el-col :span="15">
            <el-input v-model="editdataForm.terminalCode" size="mini" disabled></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="设备型号" prop="devicemodel">
          <el-col :span="15">
            <el-select
              v-model="editdataForm.devicemodel"
              placeholder="请选择"
              style="width:100%"
              size="mini"
            >
              <el-option
                v-for="item in terminaloption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
          <el-form-item label="做中继时的编号" prop="repeaterWaterCode">
          <el-col :span="15">
            <el-input v-model.number="editdataForm.repeaterWaterCode" show-word-limit size="mini"></el-input>
          </el-col>
        </el-form-item>
        <!-- <el-form-item label="ip地址" prop="ip">
          <el-col :span="15">
            <el-input v-model="editdataForm.ip" size="mini"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-col :span="15">
            <el-input v-model="editdataForm.port" size="mini"></el-input>
          </el-col>
        </el-form-item> -->
        <el-form-item label="是否启用">
          <el-col :span="15">
            <el-checkbox v-model="editdataForm.deviceisuse"></el-checkbox>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" v-if="isauth('hairdrider_edit')">
        <el-button @click="editvisible()">取消</el-button>
        <el-button type="primary" @click="editdataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
    <!-- 中继修改弹窗 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="repeatercloseDialog"
      title="修改路由信息"
      :close-on-click-modal="false"
      :visible.sync="repeaterDialogFormVisible"
    >
      <el-form
        :model="repeaterdataForm"
        :rules="repeaterdataFormRule"
        ref="repeaterdataForm"
        label-width="100px"
        :disabled="!isauth('hairdrider_edit')"
      >
        <el-form-item label="第一条中继" prop="repeater1">
          <el-col :span="15">
            <el-select
              v-model="repeaterdataForm.repeater1"
              placeholder="请选择"
              style="width:100%"
              size="mini"
              @change="repeaterslect"
              clearable
            >
              <el-option
                v-for="item in repeateroption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="第二条中继" prop="repeater2">
          <el-col :span="15">
            <el-select
              v-model="repeaterdataForm.repeater2"
              placeholder="请选择"
              style="width:100%"
              size="mini"
              @change="repeaterslect2"
              clearable
            >
              <el-option
                v-for="item in repeateroption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="第三条中继" prop="repeater3">
          <el-col :span="15">
            <el-select
              v-model="repeaterdataForm.repeater3"
              placeholder="请选择"
              style="width:100%"
              size="mini"
              @change="repeaterslect3"
              clearable
            >
              <el-option
                v-for="item in repeateroption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="第四条中继" prop="repeater4">
          <el-col :span="15">
            <el-select
              v-model="repeaterdataForm.repeater4"
              placeholder="请选择"
              style="width:100%"
              size="mini"
              @change="repeaterslect4"
              clearable
            >
              <el-option
                v-for="item in repeateroption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" v-if="isauth('hairdrider_edit')">
        <el-button @click="repeatervisible()">取消</el-button>
        <el-button type="primary" @click="repeaterdataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
    <!-- 参数模板修改弹窗 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="parametercloseDialog"
      title="修改参数模板"
      :close-on-click-modal="false"
      :visible.sync="parameterDialogFormVisible"
    >
      <el-form
        :model="parameterdataForm"
        :rules="parameterRule"
        ref="parameterdataForm"
        label-width="80px"
        :disabled="!isauth('hairdrider_edit')"
      >
        <el-form-item label="模板" prop="template">
          <el-col :span="15">
            <el-select
              v-model="parameterdataForm.template"
              placeholder="请选择"
              style="width:100%"
              size="mini"
            >
              <el-option
                v-for="item in templateoption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" v-if="isauth('hairdrider_edit')">
        <el-button @click="parametervisible()">取消</el-button>
        <el-button type="primary" @click="parameterdataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>

    <!-- 门资料管理 -->
    <el-dialog
      width="60%"
      custom-class="customWidth"
      center
      :before-close="personnelcloseDialog"
      title="门资料管理"
      :close-on-click-modal="false"
      :visible.sync="personnelFormVisible"
    >
      <!-- <door-data-management
        v-if="operatinincomesummaryofconsmptionVisible"
        ref="operatinincomesummaryofconsumption"
        :unitGuid="changeschoolid"
        :terminalGuid="terminalGuid"
      ></door-data-management> -->
    </el-dialog>
    <!-- 通讯记录 -->
    <el-dialog
      width="90%"
      custom-class="customWidth"
      center
      :before-close="recordscloseDialog"
      :title="recordsTitle"
      :close-on-click-modal="false"
      :visible.sync="recordsFormVisible"
    >
      <!-- <records-details v-if="recordsdetailsVisible" ref="recordsdetails" :roomGuid="roomGuid"></records-details> -->
    </el-dialog>
  </div>
</template>

<script>
// import DoorDataManagement from "./door-data-management";

// import RecordsDetails from "./records-details";

export default {
  data() {
    ////^([1-9]\d{0,2}|[1-4]\d{3}|5000)$/
    //网关端口检验
    let checkport = (rule, value, callback) => {
      let portReg = /^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5]$)/;
      if (value == "") {
        callback(new Error("端口不能为空"));
      } else if (!portReg.test(value)) {
        callback(new Error("端口格式错误！"));
      } else {
        callback();
      }
    };
    //中继
    let checkrepeaterid = (rule, value, callback) => {
      let repeateridReg = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)($|(?!\.$)\.)){4}$/;
      if (value == "") {
        callback(new Error("ip不能为空"));
      } else if (!repeateridReg.test(value)) {
        callback(new Error("ip格式错误！"));
      } else {
        callback();
      }
    };
    return {
      operatinincomesummaryofconsmptionVisible: true,
      // 通行记录
      recordsTitle: "房间通行记录",
      recordsdetailsVisible: true,
      datetime: [],
      consumptionpoint: "",
      // 房间id
      roomGuid: "",
      radio: 0,
      // 弹窗显示数据
      eidtDialogFormVisible: false,
      repeaterDialogFormVisible: false,
      parameterDialogFormVisible: false,
      personnelFormVisible: false,
      recordsFormVisible: false,
      terminaloption: [],
      buildingGuid: "",
      unitGuid: "",
      editdataForm: {
        terminalCode: "",
        terminalName: "",
        devicemodel: "",
        deviceisuse: false,
        terminalGuid: "",
        repeaterWaterCode:'',
        port: "",
        ip: "",
      },
      eidtataRule: {
        devicemodel: [
          { required: true, message: "设备型号必须选择", trigger: "blur" },
        ],
        // ip: [
        //   { required: true, message: "必须填写ip", trigger: "blur" },
        //   { required: true, validator: checkrepeaterid, trigger: "blur" },
        // ],
        // port: [{ required: true, validator: checkport, trigger: "blur" }],
      },

      //中继
      repeateroption: [],
      repeaterdataForm: {
        repeater1: "",
        repeater2: "",
        repeater3: "",
        repeater4: "",
        controllerGuid: "",
      },
      repeaterdataFormRule: {},

      //模板
      parameterdataForm: {
        template: "",
      },
      templateoption: [],
      parameterRule: {},

      roomGuidList: [],
      // 设备编号
      terminalGuid: "",
    };
  },
  props: ["buttonindex"],
  components: {
    // DoorDataManagement,
    // RecordsDetails
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid;
      },
    },
  },
  methods: {
    editvisible() {
      this.$refs["editdataForm"].resetFields();
      this.eidtDialogFormVisible = false;
    },
    init(item, unitGuid) {
      console.log(unitGuid);
      this.roomGuid = item.roomGuid;
      this.eidtDialogFormVisible = true;
      this.unitGuid = unitGuid;
      console.log(item);
      this.editdataForm.terminalCode = item.terminalCode;
      this.editdataForm.terminalName = item.terminalName;
      this.editdataForm.terminalGuid = item.terminalGuid;
      this.editdataForm.repeaterWaterCode = item.repeaterWaterCode;
      this.editdataForm.devicemodel = item.modelId;
      // this.editdataForm.ip = item.ip;
      // this.editdataForm.port = item.port;
      this.editdataForm.deviceisuse = item.isUse == 1 ? true : false;
      this.terminalselect();
    },
    closeDialog(done) {
      this.$refs["editdataForm"].resetFields();
      done();
    },
    //设备型号选项
    terminalselect() {
      this.terminaloption = [];
      this.$http({
        url: this.$http.adornUrl("/terminal/terminal-model-select"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            terminalType: 28,
            unitGuid: this.unitGuid,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        console.log(data);
        if (data.messageCode == 200) {
          let Arr = data.messageContent;
          if (this.terminaloption.length == 0) {
            for (var i = 0; i < Arr.length; i++) {
              var itemObj = {
                label: Arr[i].modelName,
                value: Arr[i].modelId,
              };
              this.terminaloption.push(itemObj);
            }
          }
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    //修改设备型号
    editdataFormSubmit() {
      this.$refs["editdataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              "/terminal/entrance/update-entrance-terminal-model"
            ),
            method: "post",
            data: this.$http.adornData({
              messageContent: {
                isUse: this.editdataForm.deviceisuse == true ? 1 : 0,
                unitGuid: this.unitGuid,
                terminalModel: this.editdataForm.devicemodel,
                terminalGuid: this.editdataForm.terminalGuid,
                repeaterWaterCode: parseInt(
                  this.editdataForm.repeaterWaterCode
                ),
                // ip: this.editdataForm.ip,
                // port: this.editdataForm.port,
              },
              messageType: 0,
            }),
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                type: "success",
                message: "修改成功!",
                duration: 200,
                onClose: () => {
                  this.$emit("refreshDataList", this.buttonindex + 1);
                  this.eidtDialogFormVisible = false;
                },
              });
            } else {
              this.$message.error(data.errorMessage);
            }
          });
        } else {
          return false;
        }
      });
    },
    //修改中继
    relayinit(item, unitGuid, buildingid) {
      this.roomGuidList = [];
      console.log(item);
      this.roomGuidList.push(item.roomGuid);
      this.buildingGuid = buildingid;
      this.repeaterdataForm.controllerGuid = item.controllerGuid;
      this.unitGuid = unitGuid;
      this.repeaterdataForm.terminalGuid = item.terminalGuid;
      this.repeaterdataForm.repeater1 = item.repeaterGuid1;
      this.repeaterdataForm.repeater2 = item.repeaterGuid2;
      this.repeaterdataForm.repeater3 = item.repeaterGuid3;
      this.repeaterdataForm.repeater4 = item.repeaterGuid4;
      this.repeaterlist();
      this.repeaterDialogFormVisible = true;
    },
    repeatercloseDialog(done) {
      this.$refs["repeaterdataForm"].resetFields();
      done();
    },
    repeatervisible() {
      this.$refs["repeaterdataForm"].resetFields();
      this.repeaterDialogFormVisible = false;
    },
    repeaterdataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl("/terminal/bind-terminal-room-repeater"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            repeater1: this.repeaterdataForm.repeater1,
            repeater2: this.repeaterdataForm.repeater2,
            repeater3: this.repeaterdataForm.repeater3,
            repeater4: this.repeaterdataForm.repeater4,
            roomGuidList: this.roomGuidList,
            terminalType: 28,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.$message({
            type: "success",
            message: "修改成功!",
            duration: 200,
            onClose: () => {
              this.$emit("refreshDataList", this.buttonindex + 1);
              this.repeaterDialogFormVisible = false;
            },
          });
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    repeaterlist() {
      this.repeateroption = [];
      this.$http({
        url: this.$http.adornUrl("/terminal/terminal-repeater-list"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            buildingGuid: this.buildingGuid,
            controllerGuid: this.repeaterdataForm.controllerGuid,
            terminalGuid: this.repeaterdataForm.terminalGuid,
            unitGuid: this.unitGuid,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          let Arr = data.messageContent;
          if (Arr) {
            for (var i = 0; i < Arr.length; i++) {
              var itemObj = {
                label: Arr[i].terminalName,
                value: Arr[i].terminalGuid,
              };
              this.repeateroption.push(itemObj);
            }
          }
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    repeaterslect() {
      if (this.repeaterdataForm.repeater1 == this.repeaterdataForm.repeater2) {
        this.$message({
          message: "当前选择与第二心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater1 = "";
        return;
      }
      if (this.repeaterdataForm.repeater1 == this.repeaterdataForm.repeater3) {
        this.$message({
          message: "当前选择与第三心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater1 = "";
        return;
      }
      if (this.repeaterdataForm.repeater1 == this.repeaterdataForm.repeater4) {
        this.$message({
          message: "当前选择与第四心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater1 = "";
        return;
      }
    },
    repeaterslect2() {
      if (this.repeaterdataForm.repeater2 == this.repeaterdataForm.repeater1) {
        this.$message({
          message: "当前选择与第一心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater2 = "";
        return;
      }
      if (this.repeaterdataForm.repeater2 == this.repeaterdataForm.repeater3) {
        this.$message({
          message: "当前选择与第三心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater2 = "";
        return;
      }
      if (this.repeaterdataForm.repeater2 == this.repeaterdataForm.repeater4) {
        this.$message({
          message: "当前选择与第四心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater2 = "";
        return;
      }
    },
    repeaterslect3() {
      if (this.repeaterdataForm.repeater3 == this.repeaterdataForm.repeater1) {
        this.$message({
          message: "当前选择与第一心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater3 = "";
        return;
      }
      if (this.repeaterdataForm.repeater3 == this.repeaterdataForm.repeater2) {
        this.$message({
          message: "当前选择与第二心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater3 = "";
        return;
      }
      if (this.repeaterdataForm.repeater3 == this.repeaterdataForm.repeater4) {
        this.$message({
          message: "当前选择与第四心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater3 = "";
        return;
      }
    },
    repeaterslect4() {
      if (this.repeaterdataForm.repeater4 == this.repeaterdataForm.repeater1) {
        this.$message({
          message: "当前选择与第一心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater4 = "";
        return;
      }
      if (this.repeaterdataForm.repeater4 == this.repeaterdataForm.repeater2) {
        this.$message({
          message: "当前选择与第二心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater4 = "";
        return;
      }
      if (this.repeaterdataForm.repeater4 == this.repeaterdataForm.repeater3) {
        this.$message({
          message: "当前选择与第三心跳中继冲突",
          type: "warning",
        });
        this.repeaterdataForm.repeater4 = "";
        return;
      }
    },
    //参数模板修改
    parameterinit(item, unitGuid) {
      this.roomGuidList = [];
      console.log(item.modelId);
      this.roomGuidList.push(item.roomGuid);
      this.unitGuid = unitGuid;
      this.parameterdataForm.template = item.templateGuid;
      this.terminaltemplate(item.modelId);
      this.parameterDialogFormVisible = true;
    },
    parametercloseDialog(done) {
      this.$refs["parameterdataForm"].resetFields();
      done();
    },
    parametervisible() {
      this.$refs["parameterdataForm"].resetFields();
      this.parameterDialogFormVisible = false;
    },
    //获取设备模板
    terminaltemplate(typeId) {
      this.$http({
        url: this.$http.adornUrl("/terminal/template-model-select"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            terminalType: 28,
            unitGuid: this.unitGuid,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        console.log(data);
        if (data.messageCode == 200) {
          let Arr = data.messageContent;
          if (this.templateoption.length != 0) {
            this.templateoption = [];
          }
          for (var i = 0; i < Arr.length; i++) {
            var itemObj = {
              label: Arr[i].templateName,
              value: Arr[i].templateGuid,
            };
            this.templateoption.push(itemObj);
          }
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    //修改房间参数模板
    parameterdataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl("/terminal/room-terminal-template-update"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            roomGuidList: this.roomGuidList,
            unitGuid: this.unitGuid,
            templateGuid: this.parameterdataForm.template,
            terminalType: 28,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        console.log(data);
        if (data.messageCode == 200) {
          this.$message({
            type: "success",
            message: "修改成功!",
            duration: 200,
            onClose: () => {
              this.$emit("refreshDataList");
              this.parameterDialogFormVisible = false;
            },
          });
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    // 门资料管理修改
    personnelinit(item, unitGuid, buildingid) {
      this.roomGuid = item.roomGuid;
      this.terminalGuid = item.terminalGuid;
      this.unitGuid = unitGuid;
      console.log(item, unitGuid, buildingid);
      this.personnelFormVisible = true;
      this.operatinincomesummaryofconsmptionVisible = true;
    },

    personnelcloseDialog(done) {
      this.operatinincomesummaryofconsmptionVisible = false;
      done();
    },
    // 通行记录
    // 父组件调用此函数，传值
    recordsinit(item, unitGuid, buildingid, treedata) {
      this.roomGuid = item.roomGuid;
      console.log(item, unitGuid, buildingid);
      console.log(treedata);
      this.recordsFormVisible = true;
      this.recordsdetailsVisible = true;
    },
    // 关闭窗口
    recordscloseDialog(done) {
      done();
      this.recordsdetailsVisible = false;
    },
    // 关闭窗口
    recordsvisible() {
      this.recordsFormVisible = false;
      this.recordsdetails = false;
      // 控制子节点
      this.recordsdetailsVisible = false;
    },
    // 保存
    records() {},
  },
};
</script>

<style></style>
